package GraphProperties;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import Global.ConstantVariable;
import Global.DirectoryStructureAndSettingsClass;
import Global.GlobalClass;
import SyntheticData.syntheticDataGeneratorClass;
import SyntheticData.syntheticDataGeneratorClass.TogetherFilesHolderClass;
import Utility.ListsPerGivenLevelsHolderClass;
import Utility.WorkerUtilityClass;

public class ClusteringCoefficientRelatedPropsClass extends GenericGraphPropertyClass implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4364214860322023244L;
	double[] clusteringCoefficientListOfTheNodes;
	double clusteringCoefficientOfTheGraph;
	
	double[] clusteringCoefficientDistrubutionVsDegreeLevels;
	double[] clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels; 
	
	double[] precisionIntervalsForCCDiscritization;	//asil bu.
	int lengthOfTheCCIntervals;
	
	double[] ccLevels;
	double[] ccFrequencyForTheLevels;
	
	ClusteringCoefficientRelatedPropsClass(PropertiesOfGraphClass propertiesIBelongTo)
	{	
		super(propertiesIBelongTo);		
		clusteringCoefficientOfTheGraph = 0;
	}
	
	public void setClusteringCoefficientOfTheNodesAndRelatedParameters(double[] clusteringCoefficientListOfTheNodes, double clusteringCoefficientOfTheGraph)
	{
		this.clusteringCoefficientListOfTheNodes = clusteringCoefficientListOfTheNodes;			
		this.clusteringCoefficientOfTheGraph = clusteringCoefficientOfTheGraph;
		
		calculateClusteringCoefficientVsDegreeDistribution();
		
		lengthOfTheCCIntervals = (int) Math.ceil(1.0/ConstantVariable.ClusteringCoefficentRelated_ConstantVariables.PRECISION_LEVEL_FOR_CC_DISCRETIZATON)+1;
		
		precisionIntervalsForCCDiscritization =  new double[lengthOfTheCCIntervals];
		
		analyzeClusteringCoefficientDistributionOfTheGraph();
		calculateHomophilyVsCCDistribution();		
		clusteringCoefficientCalculationIsCompleted();
	}

	public double[] getClusteringCoefficientListOfTheNodes()
	{
		return clusteringCoefficientListOfTheNodes;
	}
	
	public double getClusteringCoefficientOfTheGraph()
	{
		return clusteringCoefficientOfTheGraph;
	}
	
	void calculateClusteringCoefficientVsDegreeDistribution()
	{ 
		double[] degreeLevels=propertiesIBelongTo.degreeDistributionRelatedProps.degreeLevels;
		degreeLevels = Arrays.copyOfRange(degreeLevels, 1, degreeLevels.length);
		int numberOfDegreeLevels = degreeLevels.length;

		double[] degreeListOfTheNodes = propertiesIBelongTo.degreeDistributionRelatedProps.degreeListOfNodes;	    	

		Map<Double, Integer> degreeDistibutionMapIndex = new HashMap<Double, Integer>();

		for(int i=0; i<numberOfDegreeLevels; i++)
			degreeDistibutionMapIndex.put(degreeLevels[i], i);

		int indexOfTheDegreeDistList;

		ListsPerGivenLevelsHolderClass[] clusteringCoefficientLists = ListsPerGivenLevelsHolderClass.getNewLevelSeperatedList(numberOfDegreeLevels);


		int i=0;
		try{

			for(i=0; i<degreeListOfTheNodes.length; i++)
			{
				if(degreeListOfTheNodes[i]> 1)
				{
					indexOfTheDegreeDistList = degreeDistibutionMapIndex.get(WorkerUtilityClass.convertToSomeScale(degreeListOfTheNodes[i]));
					clusteringCoefficientLists[indexOfTheDegreeDistList].add(clusteringCoefficientListOfTheNodes[i]);				 
				}
			}
		}catch (Exception e) {
			System.out.println("i:"+i);
			System.out.println("degreeListOfTheNodes[i]:"+degreeListOfTheNodes[i]);
			e.printStackTrace();
			// TODO: handle exception
		}

		clusteringCoefficientDistrubutionVsDegreeLevels = new double[numberOfDegreeLevels];
		clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels = new double[numberOfDegreeLevels];

		double[] doubleArrayFormOfTheList; 

		for(i=0; i<clusteringCoefficientLists.length; i++)
		{
			doubleArrayFormOfTheList = WorkerUtilityClass.getDoubleArrayFromDoubleArrayList(clusteringCoefficientLists[i].getArrayListOfTheDoubleValues());
			clusteringCoefficientDistrubutionVsDegreeLevels[i]=WorkerUtilityClass.getAverage(doubleArrayFormOfTheList);
			clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels[i]=WorkerUtilityClass.findErrorBar(doubleArrayFormOfTheList);
			//System.out.println("i:"+i+":"+clusteringCoefficientLists[i].size() +":"+clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels[i]);
		}
	}

	void analyzeClusteringCoefficientDistributionOfTheGraph()
	{
		double minClusteringCoefficient = Double.MAX_VALUE;
		double maxClusteringCoefficient = -1;
		
		
		for(int i=0; i<clusteringCoefficientListOfTheNodes.length; i++)
		{
			if(clusteringCoefficientListOfTheNodes[i] > maxClusteringCoefficient)
				maxClusteringCoefficient = clusteringCoefficientListOfTheNodes[i];
			
			if(clusteringCoefficientListOfTheNodes[i] < minClusteringCoefficient)
				minClusteringCoefficient = clusteringCoefficientListOfTheNodes[i];
			
			precisionIntervalsForCCDiscritization[(int) Math.floor(clusteringCoefficientListOfTheNodes[i]*(1.0/ConstantVariable.ClusteringCoefficentRelated_ConstantVariables.PRECISION_LEVEL_FOR_CC_DISCRETIZATON))]++;
		}
		
		System.out.println("maxClusteringCoefficient:"+maxClusteringCoefficient);
		System.out.println("minClusteringCoefficient:"+minClusteringCoefficient);
		System.out.println("clusteringCoefficientOfTheGraph:"+clusteringCoefficientOfTheGraph);
		
		int sifirOlmayanCCSayisi=0;
		ArrayList<Double> ccLevelsList = new ArrayList<Double>();
		
		
		for(int i=0; i<precisionIntervalsForCCDiscritization.length; i++)
		{
			if(precisionIntervalsForCCDiscritization[i]!=0)
			{
				System.out.println(i+":"+precisionIntervalsForCCDiscritization[i]);
				sifirOlmayanCCSayisi++;
				ccLevelsList.add(WorkerUtilityClass.roundToDecimals((double)i*ConstantVariable.ClusteringCoefficentRelated_ConstantVariables.PRECISION_LEVEL_FOR_CC_DISCRETIZATON, 2));
			}
		}
		
		System.out.println("sifirOlmayanCCSayisi:"+sifirOlmayanCCSayisi);
		
		ccLevels = WorkerUtilityClass.getDoubleArrayFromDoubleArrayList(ccLevelsList);
		
		WorkerUtilityClass.printArray(ccLevels, "ccLevels");
		
		double ccOfTheElement;
		Map<Double, Integer> ccDistibutionMap = new HashMap<Double, Integer>(); 
		
		for(int i=0; i<precisionIntervalsForCCDiscritization.length; i++)
		{
			if(precisionIntervalsForCCDiscritization[i]>0)
			{
				ccOfTheElement = (double)i*ConstantVariable.ClusteringCoefficentRelated_ConstantVariables.PRECISION_LEVEL_FOR_CC_DISCRETIZATON;
				
				double size = ccOfTheElement; 
				Integer n = ccDistibutionMap.get(size); 
				if (n == null) { 
					ccDistibutionMap.put(size, 1); 
				} else { 
					ccDistibutionMap.put(size, n + 1); 
				}								
			}		
		}	
		

		System.out.println(ccDistibutionMap);
		
		System.out.println("Sorted CC hashmap START");
		
		System.out.println(Utility.WorkerUtilityClass.sortHashMapByValuesD((HashMap) ccDistibutionMap));
		
		System.out.println("Sorted CC hashmap END");	
	}
	
	void calculateHomophilyVsCCDistribution()
	{
		double[] degreeListOfTheNodes = propertiesIBelongTo.degreeDistributionRelatedProps.degreeListOfNodes;
		//double[] ccListOfTheNodes = propertiesIBelongTo.clusteringCoefficientRelatedProps.clusteringCoefficientListOfTheNodes;    	
		double[] homophilyListOfTheNodes = propertiesIBelongTo.homophilyRelatedProps.homophilyListOfTheNodes;    	
		double[] ccLevels=propertiesIBelongTo.clusteringCoefficientRelatedProps.ccLevels;
		int numberOfCCLevels = ccLevels.length;

		Map<Double, Integer> ccDistibutionMapIndex = new HashMap<Double, Integer>();

		for(int i=0; i<numberOfCCLevels; i++)
			ccDistibutionMapIndex.put(ccLevels[i], i);

		int indexOfTheCCDistList;

		ListsPerGivenLevelsHolderClass[] clusteringCoefficientLists = ListsPerGivenLevelsHolderClass.getNewLevelSeperatedList(numberOfCCLevels);


		int i=0;
		int magnifyingConstant =(int)(1.0/ConstantVariable.ClusteringCoefficentRelated_ConstantVariables.PRECISION_LEVEL_FOR_CC_DISCRETIZATON); 
		try{

			for(i=0; i<degreeListOfTheNodes.length; i++)
			{
				if(degreeListOfTheNodes[i]> 1)
				{	try
					{						
						indexOfTheCCDistList = ccDistibutionMapIndex.get((double)((int)(clusteringCoefficientListOfTheNodes[i]*magnifyingConstant))/magnifyingConstant);
						clusteringCoefficientLists[indexOfTheCCDistList].add(homophilyListOfTheNodes[i]);
					}catch (Exception e) {
						// TODO: handle exception
						System.out.println("Org deger:"+clusteringCoefficientListOfTheNodes[i]);
						System.out.println("Yuvarlanan:"+(double)((int)(clusteringCoefficientListOfTheNodes[i]*magnifyingConstant))/magnifyingConstant);
						e.printStackTrace();
					}
				}
			}
		}catch (Exception e) {
			System.out.println("i:"+i);
			System.out.println("hListOfTheNodes[i]:"+degreeListOfTheNodes[i]);
			e.printStackTrace();
			// TODO: handle exception
		}

		double[] homophilyDistrubutionVsCCLevels;
		double[] homophilyDistrubutionErrorBarsVsCCLevels;

		homophilyDistrubutionVsCCLevels = new double[numberOfCCLevels];
		homophilyDistrubutionErrorBarsVsCCLevels = new double[numberOfCCLevels];


		double[] doubleArrayFormOfTheList; 

		for(i=0; i<clusteringCoefficientLists.length; i++)
		{
			doubleArrayFormOfTheList = WorkerUtilityClass.getDoubleArrayFromDoubleArrayList(clusteringCoefficientLists[i].getArrayListOfTheDoubleValues());
			homophilyDistrubutionVsCCLevels[i]=WorkerUtilityClass.getAverage(doubleArrayFormOfTheList);
			homophilyDistrubutionErrorBarsVsCCLevels[i]=WorkerUtilityClass.findErrorBar(doubleArrayFormOfTheList);
			//System.out.println("i:"+i+":"+clusteringCoefficientLists[i].size() +":"+clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels[i]);
		}	

		propertiesIBelongTo.homophilyRelatedProps.setHomophilyvsCCRelatedParameters(homophilyDistrubutionVsCCLevels, homophilyDistrubutionErrorBarsVsCCLevels);
	}	
	
	void clusteringCoefficientCalculationIsCompleted()
	{
		String datasetName =DirectoryStructureAndSettingsClass.getDatasetName();
		
		try {
			
			/**************************PURE CC LIST*********************************/
			
			outForPureRawFile = createRawPureClusteringCoefficientResultsFile(datasetName);
			Utility.FileUtilityClass.writeArrayToFile(outForPureRawFile, clusteringCoefficientListOfTheNodes, ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForClusteringCoefficientOfNodes, "Clustering Coefficients of Nodes for " + datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType);
			outForPureRawFile.close();
			
			/**************************CC DISTRIBUTION LIST*********************************/
		
			outForRawFile = createRawClusteringCoefficientResultsFile(datasetName);
			double[] degreeLevelsValidForCC = propertiesIBelongTo.degreeDistributionRelatedProps.degreeLevels;
			degreeLevelsValidForCC = Arrays.copyOfRange(degreeLevelsValidForCC, 1, degreeLevelsValidForCC.length);	    	
			Utility.FileUtilityClass.writeTwoArraysToFile(outForRawFile, degreeLevelsValidForCC, clusteringCoefficientDistrubutionVsDegreeLevels, ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForDegreesDistributionLevelsOfNodes, ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForClusteringCoefficientDistributionOfNodes, "Clustering Coefficient Of The Graph for " + datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType);
			outForRawFile.close();
			
			/************************** CC VS DEGREE BAR PLOTS*********************************/
			
			String drawingSetting = null;
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_CC_DISTRIBUTION_VS_DEGREE_FIGURE_FOR_LINK_GRAPH;
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_CC_DISTRIBUTION_VS_DEGREE_FIGURE_FOR_CONTENT_GRAPH;
			
			outForMatlabCodeFile = createMatlabCodeClusteringCoefficientDistributionPDFResultsFile(datasetName, false);			
			createClusteringCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE, false, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.DONT_CARE, drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);			
			outForMatlabCodeFile.close();
			
			outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE);
			prepareTexFileForTheCreatedMatlabFigure();			
			outForTexForMatlabFigureFile.close();
			
			copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.CC_VS_DD_MATLAB_FIGURES_TEX_ALL_IN_ONE_FILENAME, false);
			
			/************************** CC VS DEGREE BAR TOGETHER PLOTS FOR SYNTHETIC*********************************/
			
			//TODO AS EKLEME YAPIYOR..AYNI PARAMETRELI DATASET PLOTLARINI AYNI GRAFTA YAPMA!!!
			if(GlobalClass.RunningValues.executionFor==ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
			{				
				TogetherFilesHolderClass currentTogetherFileHolder;
				currentTogetherFileHolder = syntheticDataGeneratorClass.getOutForMatlabCodeCCVsDDPDFResultsTogetherFile();
				String nameBackup = new String(matlabFileName);			
				matlabFileName = currentTogetherFileHolder.matlabFileName;
				outForMatlabCodeFile = currentTogetherFileHolder.out;				 
				createClusteringCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.DefaultScaleTypes.DEFAULT_SCALE_TYPE_TO_BE_USED_WITH_SYNTHETIC_TOGETHER_PLOTS, true, syntheticDataGeneratorClass.getGraphOrderForTogetherGraphicResults(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE, false), drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, false);
				matlabFileName = nameBackup;
			}
			
			/**************************CC VS DEGREE BAR PLOTS COMBO*********************************/
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
			{
				outForMatlabCodeFile = createMatlabCodeClusteringCoefficientDistributionPDFComboResultsFile(datasetName, false);
				createClusteringCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.FIRST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_CC_DISTRIBUTION_VS_DEGREE_COMBO_FIGURE_FOR_LINK_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);			
			}
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForMatlabCodeFile = createMatlabCodeClusteringCoefficientDistributionPDFComboResultsFile(datasetName, true);
				createClusteringCoefficientDistributionPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.LAST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_CC_DISTRIBUTION_VS_DEGREE_COMBO_FIGURE_FOR_CONTENT_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			}
				
			outForMatlabCodeFile.close();

			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE);
				prepareTexFileForTheCreatedMatlabFigure();			
				outForTexForMatlabFigureFile.close();

				copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.CC_VS_DD_MATLAB_FIGURES_TEX_COMBO_IN_ONE_FILENAME, true);	
			}
			/****************************CC VS HOMOPHILY BAR PLOTS**********************************/
						
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_HOMOPHILY_DISTRIBUTION_VS_CC_FIGURE_FOR_LINK_GRAPH;
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_HOMOPHILY_DISTRIBUTION_VS_CC_FIGURE_FOR_CONTENT_GRAPH;
			
			outForMatlabCodeFile = createMatlabCodeHomophilyDistributionVersusCCResultsFile(datasetName, false);			
			createHomophilyVsCCPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE, false, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.DONT_CARE, drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);			
			outForMatlabCodeFile.close();
			
			outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.ERRORBAR_LINEAR_SCALE);
			prepareTexFileForTheCreatedMatlabFigure();			
			outForTexForMatlabFigureFile.close();
			
			copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.CC_VS_DD_MATLAB_FIGURES_TEX_ALL_IN_ONE_FILENAME, false);
			
			/**************************HOMOPHILY VS CC BAR TOGETHER PLOTS FOR SYNTHETIC*********************************/
			
			//TODO AS EKLEME YAPIYOR..AYNI PARAMETRELI DATASET PLOTLARINI AYNI GRAFTA YAPMA!!!
			if(GlobalClass.RunningValues.executionFor==ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
			{				
				TogetherFilesHolderClass currentTogetherFileHolder;
				currentTogetherFileHolder = syntheticDataGeneratorClass.getOutForMatlabCodeHomophilyVsCCPDFResultsTogetherFile();
				String nameBackup = new String(matlabFileName);			
				matlabFileName = currentTogetherFileHolder.matlabFileName;
				outForMatlabCodeFile = currentTogetherFileHolder.out;				 
				createHomophilyVsCCPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.DefaultScaleTypes.DEFAULT_SCALE_TYPE_TO_BE_USED_WITH_SYNTHETIC_TOGETHER_PLOTS, true, syntheticDataGeneratorClass.getGraphOrderForTogetherGraphicResults(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE, false), drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, false);
				matlabFileName = nameBackup;
			}			
			
			/****************************HOMOPHILY VS CC DOT PLOT**********************************/			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_DOT_HOMOPHILY_DISTRIBUTION_VS_CC_FIGURE_FOR_LINK_GRAPH;
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
				drawingSetting = ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_DOT_HOMOPHILY_DISTRIBUTION_VS_CC_FIGURE_FOR_CONTENT_GRAPH;
			
			outForMatlabCodeFile = createMatlabCodeHomophilyDistributionVersusCCResultsFile(datasetName, false);
			createDotClusteringCoefficientvsHomophilyPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, false, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.DONT_CARE, drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			outForMatlabCodeFile.close();
			
			outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE);
			prepareTexFileForTheCreatedMatlabFigure();			
			outForTexForMatlabFigureFile.close();
			
			copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.HOMOPHILY_VS_CC_MATLAB_FIGURES_TEX_IN_ONE_FILENAME, false);
			
			/**************************HOMOPHILY VS CC DOT TOGETHER PLOTS FOR SYNTHETIC*********************************/
			//TODO AS EKLEME YAPIYOR..AYNI PARAMETRELI DATASET PLOTLARINI AYNI GRAFTA YAPMA!!!
			if(GlobalClass.RunningValues.executionFor==ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
			{
				TogetherFilesHolderClass currentTogetherFileHolder;
				currentTogetherFileHolder = syntheticDataGeneratorClass.getOutForMatlabCodeHomophilyVsCCDotPDFResultsTogetherFile();
				String nameBackup = new String(matlabFileName);			
				matlabFileName = currentTogetherFileHolder.matlabFileName;
				outForMatlabCodeFile = currentTogetherFileHolder.out;				 
				createDotClusteringCoefficientvsHomophilyPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, syntheticDataGeneratorClass.getGraphOrderForTogetherGraphicResults(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE, false), drawingSetting, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, false);
				matlabFileName = nameBackup;
			}
			
			/****************************CC VS HOMOPHILY PLOT COMBO**********************************/
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
			{
				outForMatlabCodeFile = createMatlabCodeClusteringCoefficientDistributionVersusHomophilyComboResultsFile(datasetName, false);
				createHomophilyVsCCPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.FIRST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_HOMOPHILY_DISTRIBUTION_VS_CC_COMBO_FIGURE_FOR_LINK_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			}		
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForMatlabCodeFile = createMatlabCodeClusteringCoefficientDistributionVersusHomophilyComboResultsFile(datasetName, true);
				createHomophilyVsCCPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.LAST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_HOMOPHILY_DISTRIBUTION_VS_CC_COMBO_FIGURE_FOR_CONTENT_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);				
			}
			outForMatlabCodeFile.close();
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE);
				prepareTexFileForTheCreatedMatlabFigure();			
				outForTexForMatlabFigureFile.close();
				
				copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.HOMOPHILY_VS_CC_MATLAB_FIGURES_TEX_COMBO_IN_ONE_FILENAME, true);
			}			
			
			/****************************CC VS HOMOPHILY DOT PLOT COMBO**********************************/
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE)
			{
				outForMatlabCodeFile = createMatlabCodeDotClusteringCoefficientDistributionVersusHomophilyComboResultsFile(datasetName, false);
				createDotClusteringCoefficientvsHomophilyPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.FIRST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_DOT_HOMOPHILY_DISTRIBUTION_VS_CC_COMBO_FIGURE_FOR_LINK_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);
			}		
			else if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForMatlabCodeFile = createMatlabCodeDotClusteringCoefficientDistributionVersusHomophilyComboResultsFile(datasetName, true);
				createDotClusteringCoefficientvsHomophilyPlotCodeForMatlab(outForMatlabCodeFile, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE, true, ConstantVariable.Matlab_ConstantVariables.GraphOrderType.LAST_GRAPH, ConstantVariable.Matlab_ConstantVariables.DrawingSettings.CCRelatedDrawingSettings.DRAWING_SETTING_FOR_DOT_HOMOPHILY_DISTRIBUTION_VS_CC_COMBO_FIGURE_FOR_CONTENT_GRAPH, ConstantVariable.Matlab_ConstantVariables.DistributionType.PDF_TYPE, true);				
			}
			outForMatlabCodeFile.close();
			
			if(propertiesIBelongTo.edgeType==ConstantVariable.Common_ConstantVariables.DEFAULT_EDGE_TYPE_FOR_CONTENT_GRAPH)
			{
				outForTexForMatlabFigureFile = createTexFileForTheGeneratedMatlabFigure(propertiesIBelongTo.edgeType, ConstantVariable.Matlab_ConstantVariables.ScaleType.LINEAR_SCALE);
				prepareTexFileForTheCreatedMatlabFigure();			
				outForTexForMatlabFigureFile.close();
				
				copyTexForTheFigureTo(ConstantVariable.DefaultFileNamesConstants.DefaultAllInOneResultsFiles.HOMOPHILY_VS_CC_MATLAB_FIGURES_TEX_COMBO_IN_ONE_FILENAME, true);
			}
			
		} catch (IOException e) {			
			e.printStackTrace();
		}		
	}	
	
    public BufferedWriter createRawPureClusteringCoefficientResultsFile(String datasetName)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.CLUSTERING_COEFFICIENT_RELATED_DATA_FOLDER_NAME    				
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.RAW_DATA_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"ClusteringCoefficientsOfNodes.txt"; 
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    /**********************************************/
	
    public BufferedWriter createRawClusteringCoefficientResultsFile(String datasetName)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.CLUSTERING_COEFFICIENT_RELATED_DATA_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.RAW_DATA_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR+propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR+"ClusteringCoefficientVsDegreeDistribution.txt"; 
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    /*******************Matlab Files for Clustering Coefficient Versus Degree Distribution***************************/
         
    public BufferedWriter createMatlabCodeClusteringCoefficientDistributionPDFResultsFile(String datasetName, boolean appendMode)
    {    	
		return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.CLUSTERING_COEFFICIENT_DISTRIBUTION_PDF_RESULTS_FILE_SUFFIX, appendMode, true);
    }
    
    public BufferedWriter createMatlabCodeClusteringCoefficientDistributionPDFComboResultsFile(String datasetName, boolean appendMode)
    {    	
		return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.CLUSTERING_COEFFICIENT_DISTRIBUTION_PDF_COMBO_RESULTS_FILE_SUFFIX, appendMode, false);
    }
        
    /*******************Matlab Files for Clustering Coefficient Versus Homophily***************************/
    
    public BufferedWriter createMatlabCodeHomophilyDistributionVersusCCResultsFile(String datasetName, boolean appendMode)
    {
    	return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.HOMOPHILY_DISTRIBUTION_VERSUS_CLUSTERING_COEFFICIENT_RESULTS_FILE, appendMode, true);    	
    }    

    public BufferedWriter createMatlabCodeDotHomophilyDistributionVersusCCResultsFile(String datasetName, boolean appendMode)
    {
    	return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.HOMOPHILY_DOT_DISTRIBUTION_VERSUS_CLUSTERING_COEFFICIENT_RESULTS_FILE, appendMode, true);    	
    }
    
    public BufferedWriter createMatlabCodeClusteringCoefficientDistributionVersusHomophilyComboResultsFile(String datasetName, boolean appendMode)
    {
    	return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.HOMOPHILY_DISTRIBUTION_VERSUS_CLUSTERING_COEFFICIENT_COMBO_RESULTS_FILE, appendMode, false);
    }
    
    public BufferedWriter createMatlabCodeDotClusteringCoefficientDistributionVersusHomophilyComboResultsFile(String datasetName, boolean appendMode)
    {
    	return createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(datasetName, ConstantVariable.GraphPropertiesSuffixes.CCPropertiesSuffixes.HOMOPHILY_DOT_DISTRIBUTION_VERSUS_CLUSTERING_COEFFICIENT_COMBO_RESULTS_FILE, appendMode, false);
    }
     
    
    /*******************************************************************************************************/    
    
    public BufferedWriter createMatlabCodeFilesUnderMatlabCodeDirectoryForClusteringCoefficent(String datasetName, String suffixForTheFileName, boolean appendMode, boolean includeEdgeType)
    {
    	String path= DirectoryStructureAndSettingsClass.getFullPathOfTheRootDirectoryNameForTheCurrentDataset()
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.GRAPH_PROPERTIES_FOLDER_NAME
    				+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGraphPropertiesDirectory.CLUSTERING_COEFFICIENT_RELATED_DATA_FOLDER_NAME;
    				    				    	
    	fullPathOfParentDirectoryOfMatlabCode = path;    	
    	path = path + ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderGenericGraphPropertyDirectory.MATLAB_CODE_FOLDER_NAME;
    	
    	String fileName = datasetName+ConstantVariable.Format_ConstantVariables.DASH_CHAR;
    	if(includeEdgeType)
    		fileName += propertiesIBelongTo.edgeType+ConstantVariable.Format_ConstantVariables.DASH_CHAR;
    	fileName +=suffixForTheFileName;    	
    	 
    	matlabFileName = fileName;
    	fullPathOfTheMatlabFile = path;
    	
    	fileName += ".m";
    	
    	try {
    		
    		File descFile=DirectoryStructureAndSettingsClass.createFile(path, fileName);
    		FileWriter fstream;
    		fstream = new FileWriter(descFile, appendMode);
    		BufferedWriter out = new BufferedWriter(fstream);
    		return out;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    /*******************Clustering Coefficient Plot Codes for Matlab***************************/
    
    void createClusteringCoefficientDistributionPlotCodeForMatlab(BufferedWriter outForMatlabCodeFile, int type, boolean isForCombo, int order, String drawingSettings, int distributionPlotType, boolean generateFigure)
    {    	
    	double[] degreeLevels = propertiesIBelongTo.degreeDistributionRelatedProps.degreeLevels;    	
    	degreeLevels = Arrays.copyOfRange(degreeLevels, 1, degreeLevels.length);
    	createPlotCodeForMatlab(outForMatlabCodeFile, type, isForCombo, order, degreeLevels, clusteringCoefficientDistrubutionVsDegreeLevels, clusteringCoefficientDistrubutionErrorBarsVsDegreeLevels, "Clustering Coefficient vs Degree Distribution", "Degree (D)", "Clustering Coefficient (C)", drawingSettings, distributionPlotType, generateFigure);
    }
    
    void createDotClusteringCoefficientvsHomophilyPlotCodeForMatlab(BufferedWriter outForMatlabCodeFile, int type, boolean isForCombo, int order, String drawingSettings, int distributionPlotType, boolean generateFigure)
    {    	
    	double[] homophilyListOfTheNodes = propertiesIBelongTo.homophilyRelatedProps.getHomophilyListOfTheNodes();
    	double[] cleanedHomophilyListOfTheNodes = propertiesIBelongTo.prepareForCorrelationCalculation(homophilyListOfTheNodes, 2);
    	double[] cleanedCCListOfTheNodes = propertiesIBelongTo.prepareForCorrelationCalculation(clusteringCoefficientListOfTheNodes, 2);
    	createPlotCodeForMatlab(outForMatlabCodeFile, type, isForCombo, order, cleanedCCListOfTheNodes, cleanedHomophilyListOfTheNodes, null, "Homophily vs Clustering Coefficient", "Clustering Coefficient (C)", "Homophily (H)", drawingSettings, distributionPlotType, generateFigure);
    }
    
    void createHomophilyVsCCPlotCodeForMatlab(BufferedWriter outForMatlabCodeFile, int type, boolean isForCombo, int order, String drawingSettings, int distributionPlotType, boolean generateFigure)
    {   
    	double[] homophilyDistrubutionVsCCLevels = propertiesIBelongTo.homophilyRelatedProps.homophilyDistrubutionVsCCLevels;
    	double[] homophilyDistrubutionErrorBarsVsCCLevels = propertiesIBelongTo.homophilyRelatedProps.homophilyDistrubutionErrorBarsVsCCLevels;
    	
    	createPlotCodeForMatlab(outForMatlabCodeFile, type, isForCombo, order, ccLevels, homophilyDistrubutionVsCCLevels, homophilyDistrubutionErrorBarsVsCCLevels, "Homophily vs Clustering Coefficient", "Clustering Coefficient (C)", "Homophily (H)", drawingSettings, distributionPlotType, generateFigure);
    }
}
